//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Events;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class CheckoutOnePageControl : BaseNopFrontendUserControl
    {
        protected ShoppingCart _cart = null;

        protected enum CheckoutStepEnum
        {
            ShippingAddress,
            BillingAddress,
            ShippingMethod,
            PaymentMethod,
            PaymentInfo,
            Confirm
        }

        protected void SelectPane(CheckoutStepEnum step) {

            ctrlCheckoutShippingAddress.BindData();
            ctrlCheckoutBillingAddress.BindData();
            ctrlCheckoutShippingMethod.BindData();
            ctrlCheckoutPaymentMethod.BindData();
            ctrlCheckoutPaymentInfo.LoadPaymentControl();
            ctrlCheckoutPaymentInfo.BindData();
            ctrlCheckoutConfirm.BindData();
            OrderSummaryControl.BindData();
        }

        public bool IsPaymentWorkflowRequired() {
            bool result = true;

            //check whether order total equals zero
            if (NopContext.Current.User != null) {
                decimal? shoppingCartTotalBase = this.ShoppingCartService.GetShoppingCartTotal(this.Cart,
                NopContext.Current.User.LastPaymentMethodId, NopContext.Current.User);

                if (shoppingCartTotalBase.HasValue && shoppingCartTotalBase.Value == decimal.Zero) {
                    result = false;
                }
            }
            return result;
        }

        protected void Page_Load(object sender, EventArgs e) {
            if ((NopContext.Current.User == null) || (NopContext.Current.User.IsGuest && !this.CustomerService.AnonymousCheckoutAllowed)) {
                string loginURL = SEOHelper.GetLoginPageUrl(true);
                Response.Redirect(loginURL);
            }

            if (Cart.Count == 0) {
                Response.Redirect(SEOHelper.GetShoppingCartUrl());
            }

            //validation
            var scWarnings = this.ShoppingCartService.GetShoppingCartWarnings(Cart, NopContext.Current.User.CheckoutAttributes, true);
            if (scWarnings.Count > 0) {
                Response.Redirect(SEOHelper.GetShoppingCartUrl());
            } else {
                foreach (ShoppingCartItem sci in this.Cart) {
                    List<String> sciWarnings = this.ShoppingCartService.GetShoppingCartItemWarnings(
                        sci.ShoppingCartType,
                        sci.ProductVariantId,
                        sci.AttributesXml,
                        sci.CustomerEnteredPrice,
                        sci.Quantity);
                    if (sciWarnings.Count > 0) {
                        Response.Redirect(SEOHelper.GetShoppingCartUrl());
                    }
                }
            }

            if (!Page.IsPostBack) {
                if (!this.ShippingService.ShoppingCartRequiresShipping(Cart)) {
                    //pnlShippingAddress.Visible = false;
                    //pnlShippingMethods.Visible = false;
                }

                SelectPane(CheckoutStepEnum.ShippingAddress);
            }
        }

        protected void ctrlCheckoutShippingAddress_CheckoutStepChanged(object sender, CheckoutStepEventArgs e) {
            if (e.ShippingAddressSelected) {
                SelectPane(CheckoutStepEnum.BillingAddress);
            }
        }

        protected void ctrlCheckoutBillingAddress_CheckoutStepChanged(object sender, CheckoutStepEventArgs e) {
            if (e.BillingAddressSelected) {
                SelectPane(CheckoutStepEnum.ShippingMethod);
            }
        }

        protected void ctrlCheckoutShippingMethod_CheckoutStepChanged(object sender, CheckoutStepEventArgs e) {
            if (e.ShippingMethodSelected) {
                SelectPane(CheckoutStepEnum.PaymentMethod);
            }
        }

        protected void ctrlCheckoutPaymentMethod_CheckoutStepChanged(object sender, CheckoutStepEventArgs e) {
            if (e.PaymentMethodSelected) {
                SelectPane(CheckoutStepEnum.PaymentInfo);
            }
        }

        protected void ctrlCheckoutPaymentInfo_CheckoutStepChanged(object sender, CheckoutStepEventArgs e) {
            if (e.PaymentInfoEntered) {
                SelectPane(CheckoutStepEnum.Confirm);
            }
        }

        protected void ctrlCheckoutConfirm_CheckoutStepChanged(object sender, CheckoutStepEventArgs e) {
            if (e.OrderConfirmed) {
                Response.Redirect("~/checkoutcompleted.aspx");
            } else {
                SelectPane(CheckoutStepEnum.PaymentInfo);
            }
        }

        protected void BtnModifyShippingAddress_OnClick(object sender, EventArgs e) {
            SelectPane(CheckoutStepEnum.ShippingAddress);
        }

        protected void BtnModifyBillingAddress_OnClick(object sender, EventArgs e) {
            SelectPane(CheckoutStepEnum.BillingAddress);
        }

        protected void BtnModifyShippingMethod_OnClick(object sender, EventArgs e) {
            SelectPane(CheckoutStepEnum.ShippingMethod);
        }

        protected void BtnModifyPaymentMethod_OnClick(object sender, EventArgs e) {
            SelectPane(CheckoutStepEnum.PaymentMethod);
        }

        protected void BtnModifyPaymentInfo_OnClick(object sender, EventArgs e) {
            SelectPane(CheckoutStepEnum.PaymentInfo);
        }

        public ShoppingCart Cart {
            get {
                if (_cart == null) {
                    _cart = this.ShoppingCartService.GetCurrentShoppingCart(ShoppingCartTypeEnum.ShoppingCart);
                }
                return _cart;
            }
        }
    }
}